package com.ruoyi.web.controller.client;/**
 * Created by XiaoYi  on 2020-04-01 16:20
 */

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.Global;
import com.ruoyi.common.config.YfGlobal;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.GetUserId;
import com.ruoyi.common.enums.OperatorType;
import com.ruoyi.common.enums.RequestType;
import com.ruoyi.common.exception.file.InvalidExtensionException;
import com.ruoyi.common.exception.user.UserBlockedException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.file.MimeTypeUtils;
import com.ruoyi.framework.responseBodyJson.JSON;
import com.ruoyi.framework.shiro.service.SysLoginService;
import com.ruoyi.system.domain.SysDept;
import com.ruoyi.system.domain.SysUser;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.yf.domain.YfClientVersion;
import com.ruoyi.yf.domain.YfHospitalStatus;
import com.ruoyi.yf.domain.YfOutstoreRecord;
import com.ruoyi.yf.domain.YfTag;
import com.ruoyi.yf.domain.mobile.*;
import com.ruoyi.yf.service.IYfClientVersionService;
import com.ruoyi.yf.service.IYfStorageWarningService;
import com.ruoyi.yf.service.IYfTagService;
import com.ruoyi.yf.tool.barcode.Rules;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * XiaoYi...
 * 接口 - 单条医废采集APP
 * fea4d85f-2215-4c70-b020-c51ad0bdbb4a
 */
@RequestMapping("/client/report")
@RestController
@Api(tags = "接口-单条医废采集APP")
public class ClientReportController extends BaseController {

    @Autowired
    private SysLoginService loginService;

    @Autowired
    private IYfClientVersionService yfClientVersionService;

    @Autowired
    private IYfTagService yfTagService;

    @Autowired
    private ISysDeptService deptService;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


    @ApiOperation(value = "接口-测试", notes = "测试类", position = 1)
    @RequestMapping(method = {RequestMethod.POST, RequestMethod.GET}, value = "/test")
    public AjaxResult postTest(@RequestParam(required = false) String str) {
        if (str == null)
            return AjaxResult.result("200", -1);
        return AjaxResult.success("请求成功！", str);
    }


    // 统计接口
    //86645427-70a9-45f2-bfc0-1ad3ecfd0969
    @ApiOperation(value = "接口-医废类型日统计查询", notes = "医废类型统计", position = 3, produces = "application/json")
    @RequestMapping(method = RequestMethod.POST, value = "/statistical")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "uid", value = "用户id", type = "Long", required = true),
            @ApiImplicitParam(name = "date", value = "查询日期yyyy-MM-dd", dataType = "Date", required = true)
    })
    @Log(title = "接口-医废类型日统计查询", businessType = BusinessType.SELECT, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.UID)
    public List<YfTypeStatis> listClient(String uid, String date) {
        List<YfTypeStatis> list = new ArrayList<>();
        try {
            new SimpleDateFormat("yyyy-MM-dd").parse(date);
        } catch (Exception e) {
            return list;
        }
        list.addAll(yfTagService.selectUnionAll(uid, date));
        return list;
    }


    //f14616d3-a7b6-428e-bc9f-96b712872290
    @ApiOperation(value = "接口-登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "账号", type = "String", required = true),
            @ApiImplicitParam(name = "password", value = "密码", type = "String", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/login")
    public SysUser login(String username, String password) {
        SysUser user = new SysUser();
        SysUser login;
        try {
            login = loginService.login(username, password);
            user.setUserId(login.getUserId());
            user.setUserName(login.getUserName());
            user.setSex(login.getSex());
            user.setPassword(password);
            user.setStatus(login.getStatus());
            user.setDeptId(login.getDeptId());
            user.setDeptName(login.getDept().getDeptName());
            user.setLoginName(login.getLoginName());
            user.setPhonenumber(login.getPhonenumber());
            user.setEmail(login.getEmail());
            user.setAvatar(login.getAvatar());
            user.setDeptStatus(login.getDept().getStatus());    // 暂存点状态
            user.setRoles(login.getRoles());        // 角色list，一个用户可拥有多个角色
            // 如果dept是社区服务中心
            if (login.getDept().getDeptType().equals("310")) {
                user.setHospitalId(login.getDeptId());
                user.setHospitalName(login.getDeptName());
                user.setHospitalStatus(login.getDept().getStatus());
            } else {  // 查询暂存点的父类医院
                SysDept hospital = deptService.selectDeptById(login.getDept().getParentId());
                user.setHospitalId(hospital.getDeptId());
                user.setHospitalName(hospital.getDeptName());
                user.setHospitalStatus(hospital.getStatus());
            }
        } catch (UserBlockedException e) {
            user.setStatus("1");
        }

        // 根据暂存点id去视图查询医院id
//        SysDept hospital = yfTagService.getHospitalByStaging(user.getDeptId());
//        user.setHospitalId(hospital.getDeptId());
//        user.setHospitalName(hospital.getDeptName());

        return user;
    }


    // 查询app最新版本
    //eda3d51c-84cd-472a-9301-91492d2cccd5
    @ApiOperation(value = "接口-查询app最新版本")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "app类型", type = "String", required = true),
            @ApiImplicitParam(name = "updateRegion", value = "更新地区", type = "String", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/getAppVersion")
    public YfClientVersion getAppVersion(String type, String updateRegion) {
        YfClientVersion yfcv = yfClientVersionService.getAppVersionByTypeAndRegion(type, updateRegion);
        return yfcv;
    }


    // 查询今天的各医废状态袋数
    //8f9bfb24-117f-4039-8a70-091ebcda2997
    @ApiOperation(value = "接口-查询今天的各医废状态袋数")
    @RequestMapping(method = RequestMethod.POST, value = "/getTodayYFNum")
    @ApiImplicitParam(name = "uid", value = "用户id", dataType = "Long")
    public HashMap<String, Object> getTodayYFNum(Long uid) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (uid == null) {
            TableDataInfo dataTable = new TableDataInfo();
            dataTable.setMsg("请检查参数");
            dataTable.setCode(1001);
            hashMap.put("dataTable", dataTable);
            return hashMap;
        }
        String num = yfTagService.getTodayYFNum(uid);
        String[] todayYFNum = num.split(",");
        hashMap.put("todayCollNum", todayYFNum[0]);
        hashMap.put("todayNoTransNum", todayYFNum[1]);
        hashMap.put("todayHasTransNum", todayYFNum[2]);
        hashMap.put("todayInStoreNum", todayYFNum[3]);
        hashMap.put("todayOutStoreNum", todayYFNum[4]);
        return hashMap;
    }


    // 提交单条医废接口
    //8b7d1000-3a5b-4f76-a846-25e3dd9e7b19
    @ApiOperation(value = "接口-提交单条医废")
    @ApiImplicitParam(name = "yfTag", value = "对象", dataType = "YfTag", allowMultiple = true)
    @RequestMapping(method = RequestMethod.POST, value = "/saveYF")
    @Log(title = "接口-提交单条医废", businessType = BusinessType.INSERT, operatorType = OperatorType.MOBILE,
            requestType = RequestType.JSON, getUserId = GetUserId.JSONONEYFTAG)
    public HashMap<String, Object> saveYF(@RequestBody YfTag yfTag) {
        HashMap<String, Object> hashMap = new HashMap<>();
        // 即时提交时取服务器时间，app延时提交时取app提交的时间，取收集时间进行判断是否延时提交
        Date date = new Date();
        if (yfTag.getCollectUpdateDate() == null) {     // 为空是即时提交
            if (yfTag.getTrashStatus().equals("10")) {  // 仅收集
                yfTag.setCollectUpdateDate(date);   // 即时提交时取服务器时间，收集人app已传值
            } else if (yfTag.getTrashStatus().equals("15")) {   //  收集 + 转运
                yfTag.setCollectUpdateDate(date);       // 收集员、收集时间、转运员、转运时间
                yfTag.setTransUserId(yfTag.getCollectUserId());
                yfTag.setTransDate(date);
                yfTag.setTrashStatus("10");     // 收集 + 转运 记录成 10 收集状态
            } else if (yfTag.getTrashStatus().equals("20")) {   //  收集 + 转运 + 入库
                yfTag.setCollectUpdateDate(date);       // 收集员、收集时间、转运员、转运时间、入库员、入库时间
                yfTag.setTransUserId(yfTag.getCollectUserId());
                yfTag.setTransDate(date);
                yfTag.setIntoUserId(yfTag.getCollectUserId());
                yfTag.setIntoDate(date);
            }
        } else {      // 不为空时是延时提交 , createTime用系统时间 , 收集时间、转运时间、入库时间app提交
            logger.debug(" --- saveYF : 医废收集时间 = " +
                    new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(yfTag.getCollectUpdateDate()));
            if (yfTag.getTrashStatus().equals("15")) {   //  收集 + 转运
                // 收集员、收集时间、转运员、转运时间
                yfTag.setTransUserId(yfTag.getCollectUserId());
                yfTag.setTransDate(yfTag.getCollectUpdateDate());
                yfTag.setTrashStatus("10");     // 收集 + 转运 记录成 10 收集状态
            } else if (yfTag.getTrashStatus().equals("20")) {   //  收集 + 转运 + 入库
                // 收集员、收集时间、转运员、转运时间、入库员、入库时间
                yfTag.setTransUserId(yfTag.getCollectUserId());
                yfTag.setTransDate(yfTag.getCollectUpdateDate());
                yfTag.setIntoUserId(yfTag.getCollectUserId());
                yfTag.setIntoDate(yfTag.getCollectUpdateDate());
            }
        }
        // 重量输入方式
        if( StringUtils.isEmpty(yfTag.getWeightInputType()) ){
            yfTag.setWeightInputType("0");
        }
        // 更新方式 , 客户端上传
        yfTag.setUpdateWay("0");
        // 添加到数据库，上传成功返回医废条码
        try {
            int i = yfTagService.insertYfTag(yfTag);
            if (i < 0) {
                hashMap.put("resultCode", "N");
                hashMap.put("msg", "上传失败");
                hashMap.put("codeid", yfTag.getCodeid());
            } else {
                hashMap.put("resultCode", "Y");
                hashMap.put("msg", "上传成功");
                hashMap.put("codeid", yfTag.getCodeid());
                hashMap.put("collDateStr", sdf.format(date));
            }
        } catch (DuplicateKeyException e) {
            // 主键冲突异常 , 认为此条医废成功提交
            logger.debug(" --- saveYF : 医废已上传 !   条码： " + yfTag.getCodeid());
            hashMap.put("resultCode", "Y");
            hashMap.put("msg", "医废已上传");
            hashMap.put("codeid", yfTag.getCodeid());
        }

        return hashMap;
    }


    // 上传医废照片  flag = 10、15、20、30  分别是 收集、转运、入库、出库 时的照片
    //2322aa56-55cf-4058-8b33-d7dc1366d492
    @Log(title = "接口-上传医废照片", businessType = BusinessType.INSERT)
    @PostMapping("/saveIMG")
    @ResponseBody
    public HashMap<String, Object> saveIMG(String codeId, String userId, String yfStatusFlag,
                                           @RequestParam(value = "img", required = false) MultipartFile file) {
        HashMap<String, Object> hashMap = new HashMap<>();
        // 检查参数是否完整
        if (StringUtils.isEmpty(codeId) || StringUtils.isEmpty(userId) || StringUtils.isEmpty(userId)) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数填写不完整");
            return hashMap;
        }
        // 检查是否上传了照片
        if (file == null || file.isEmpty()) {
            logger.debug(" ------   没有医废照片 ！  ");
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "没有医废照片");
        }

        // 查询条码是否存在，不存在可能条码还没上传，条码存在再检查是否已上传图片，已有图片直接返回上传成功
        YfTag yf = yfTagService.selectYfTagById(codeId);
        if (yf == null || yf.getCodeid() == null || yf.getCodeid().trim().equals("")) {
            logger.debug(" ------   医废条码不存在 ！  ");
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "医废条码不存在");
            hashMap.put("codeid", codeId);
            hashMap.put("yfStatusFlag", yfStatusFlag);
            return hashMap;
        }
        // yfStatusFlag = 10时 判断 img1 是否已有值，有值说明图片已上传； 15 ： img2   20 ：img3    30 ： img4
        if ((yfStatusFlag.equals("10") && !StringUtils.isEmpty(yf.getImg1()))
                || (yfStatusFlag.equals("15") && !StringUtils.isEmpty(yf.getImg2()))
                || (yfStatusFlag.equals("20") && !StringUtils.isEmpty(yf.getImg3()))
                || (yfStatusFlag.equals("30") && !StringUtils.isEmpty(yf.getImg4()))) {
            logger.debug(" ------   医废照片已上传 ！  ");
            hashMap.put("resultCode", "Y");
            hashMap.put("msg", "医废照片已上传");
            hashMap.put("codeid", codeId);
            hashMap.put("yfStatusFlag", yfStatusFlag);
            return hashMap;
        }

        // 保存img 返回图片地址
        String imgPath = saveImgAndBackPath(file, userId, codeId, 1);
        yfTagService.saveIMG(codeId, imgPath, yfStatusFlag);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "上传医废照片成功");
        hashMap.put("codeid", codeId);
        hashMap.put("yfStatusFlag", yfStatusFlag);
        return hashMap;
    }


    // 保存医废收集页面第二张照片
    @Log(title = "接口-上传医废第二张照片", businessType = BusinessType.INSERT)
    @PostMapping("/saveCollPhoto2")
    @ResponseBody
    public HashMap<String, Object> saveCollPhoto2(String codeId, String userId,
                                           @RequestParam(value = "img", required = false) MultipartFile file) {
        HashMap<String, Object> hashMap = new HashMap<>();
        // 检查参数是否完整
        if (StringUtils.isEmpty(codeId) || StringUtils.isEmpty(userId)) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数填写不完整");
            return hashMap;
        }
        // 检查是否上传了照片
        if (file == null || file.isEmpty()) {
            logger.debug(" ------   没有医废第二张照片 ！  ");
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "没有医废第二张照片");
        }

        String imgPath = saveImgAndBackPath(file, userId, codeId, 2);
        yfTagService.saveCollPhoto2(codeId, imgPath);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "上传医废照片成功");
        hashMap.put("codeid", codeId);
        return hashMap;
    }


    /**
     * 保存照片
     * 3f07e07b-6942-4be0-989c-8815af7e128b
     */
    public String saveImgAndBackPath(MultipartFile file, String userId, String codeId, int num) {
        String imgPath = "";
        String fileNames;
        if( num == 1 ){
            fileNames = "IMG" + "_" + userId + "_" + codeId + "_" + new SimpleDateFormat("HHmmss").format(new Date());
        }else{
            fileNames = "IMG" + "_2_" + userId + "_" + codeId + "_" + new SimpleDateFormat("HHmmss").format(new Date());
        }
        try {
            imgPath = FileUploadUtils.upload(Global.getProfile(),
                    "/" + YfGlobal.getImgPath() + "/" + new SimpleDateFormat("yyyy/MM/dd").format(new Date()) + "/" + userId,
                    file, MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, fileNames);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InvalidExtensionException e) {
            e.printStackTrace();
        }
        return imgPath;
    }


    // 根据条码和暂存点获取医废信息
    //383d1611-57bc-4675-9715-fd952a149d8d
    @ApiOperation(value = "接口-根据条码获取医废信息，联查各状态人员姓名")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codeId", value = "条码", dataType = "String", required = true),
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", dataType = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/getYFInfoByBarcodeAndStagingId")
    public YfTag getYFInfoByBarcodeAndStagingId(String codeId, Long stagingId) {
        YfTag yfTag = yfTagService.getYFInfoByBarcodeAndStagingId(codeId, stagingId);
        return yfTag;
    }


    // 根据条码获取医废信息
    //c6325615-c9de-4ef3-8e5f-402bed9dfffc
    @ApiOperation(value = "接口-根据条码获取医废信息，联查各状态人员姓名")
    @ApiImplicitParam(name = "codeId", value = "条码", dataType = "String", required = true)
    @RequestMapping(method = RequestMethod.POST, value = "/getYFInfoByBarcode")
    public YfTag getYFInfoByBarcode(String codeId) {
        YfTag yfTag = yfTagService.getYFInfoByBarcode(codeId);
        return yfTag;
    }


    // 收集 -> 转运 医废
    //4e3d508b-acf8-4548-8fb2-fba672da09a5
    @ApiOperation(value = "接口-医废收集->转运")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codeId", value = "条码", type = "String", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/collToTransYF")
    @Log(title = "接口-医废收集->转运", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> collToTransYF(String codeId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (StringUtils.isEmpty(codeId) || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有已收集状态才能修改成已转运状态
        yfTagService.collToTransYF(codeId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "转运成功");
        return hashMap;
    }


    // 收集 -> 入库 医废
    //efadba63-7fe3-484c-87c3-b644288a5ee2
    @ApiOperation(value = "接口-医废收集->入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codeId", value = "条码", type = "String", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/collToInStoreYF")
    @Log(title = "接口-医废收集->入库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> collToInStoreYF(String codeId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (StringUtils.isEmpty(codeId) || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有已收集状态才能修改成已入库状态
        yfTagService.collToInStoreYF(codeId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "入库成功");
        return hashMap;
    }


    // 转运 -> 入库 医废
    //fb91d8d3-7f92-462a-83da-4b0d583169b5
    @ApiOperation(value = "接口-医废转运->入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codeId", value = "条码", type = "String", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/transToInStoreYF")
    @Log(title = "接口-医废转运->入库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> transToInStoreYF(String codeId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (StringUtils.isEmpty(codeId) || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有已转运状态才能修改成已入库状态
        yfTagService.transToInStoreYF(codeId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "入库成功");
        return hashMap;
    }


    // 入库 -> 出库 医废
    //fa23dcbf-bdaf-4cc7-88fe-28e9cd4ff584
    @ApiOperation(value = "接口-医废入库->出库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "codeId", value = "条码", type = "String", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/inStoreToOutStoreYF")
    @Log(title = "接口-医废入库->出库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> inStoreToOutStoreYF(String codeId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (StringUtils.isEmpty(codeId) || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有已入库状态才能修改成已出库状态
        yfTagService.inStoreToOutStoreYF(codeId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "出库成功");
        return hashMap;
    }


    // 一键入库 收集 -> 入库
    //8cb47303-9523-4be0-891c-be127d4e02b0
    @ApiOperation(value = "接口-一键入库：收集->入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/oneKeyCollToInStoreYF")
    @Log(title = "接口-一键入库：收集->入库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> oneKeyCollToInStoreYF(Long stagingId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (stagingId == null || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有已收集状态才能修改成已入库状态
        yfTagService.oneKeyCollToInStoreYF(stagingId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "入库成功");
        return hashMap;
    }


    // 一键入库 转运 -> 入库
    //27a906af-7fde-4e4c-9dda-7066d497b022
    @ApiOperation(value = "接口-一键入库：转运->入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/oneKeyTransToInStoreYF")
    @Log(title = "接口-一键入库：转运->入库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> oneKeyTransToInStoreYF(Long stagingId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (stagingId == null || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有已转运状态才能修改成已入库状态
        yfTagService.oneKeyTransToInStoreYF(stagingId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "入库成功");
        return hashMap;
    }


    // 一键出库 入库 -> 出库
    // 818e69d9-e169-465b-a131-bf4eb533cfd5
    @ApiOperation(value = "接口-一键出库：入库->出库 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/oneKeyOutStoreYF")
    @Log(title = "接口-一键出库：入库->出库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> oneKeyOutStoreYF(Long stagingId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (stagingId == null || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有已入库状态才能修改成已出库状态
        yfTagService.oneKeyOutStoreYF(stagingId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "出库成功");
        return hashMap;
    }


    // 根据医废状态、用户等查询当天医废汇总
    //e021a8a2-c68f-4be6-950b-ebf701313f8f
    @ApiOperation(value = "接口-当天医废汇总 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true),
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "hospitalId", value = "医院id", type = "Long", required = true),
            @ApiImplicitParam(name = "yfStatus", value = "医废状态", type = "String", required = true)

    })
    @RequestMapping(method = RequestMethod.POST, value = "/getDayYFStatusStatis")
    @Log(title = "接口-当天医废汇总", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public List<YfTypeStatis> getDayYFStatusStatis(Long userId,Long stagingId,Long hospitalId,String yfStatus ) {
        if ( userId == null || stagingId == null || hospitalId == null || StringUtils.isEmpty(yfStatus) ) {
            return new ArrayList<>();
        }

        List<YfTypeStatis> list = yfTagService.getDayYFStatusStatis(userId, stagingId, hospitalId, yfStatus,
                new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
        return list;
    }


    // 根据暂存点id查询已收集医废(按收集员姓名分组)  已收集的
    //eee741ed-dd86-4b5e-8bf5-094924fcc8f6
    @ApiOperation(value = "接口-根据条码获取医废信息，联查各状态人员姓名")
    @ApiImplicitParam(name = "stagingId", value = "暂存点id", dataType = "Long", required = true)
    @RequestMapping(method = RequestMethod.POST, value = "/getAlreadyCollYFByDeptId")
    public List<YfUserBag> getAlreadyCollYFByDeptId(Long stagingId) {
        List<YfUserBag> list = yfTagService.getAlreadyCollYFByDeptId(stagingId);
        return list;
    }


    // 根据暂存点id查询已转运医废(按转运员姓名分组)  已转运的
    //33f01428-dac9-4359-b25c-4f7b38f44a2a
    @ApiOperation(value = "接口-根据条码获取医废信息，联查各状态人员姓名")
    @ApiImplicitParam(name = "stagingId", value = "暂存点id", dataType = "Long", required = true)
    @RequestMapping(method = RequestMethod.POST, value = "/getAlreadyTransYFByDeptId")
    public List<YfUserBag> getAlreadyTransYFByDeptId(Long stagingId) {
        List<YfUserBag> list = yfTagService.getAlreadyTransYFByDeptId(stagingId);
        return list;
    }


    // 根据暂存点id查询已入库医废(按入库员姓名分组)  已入库的
    //6fc3b685-b5d5-49bb-b6c6-ca29a72d023c
    @ApiOperation(value = "接口-根据条码获取医废信息，联查各状态人员姓名")
    @ApiImplicitParam(name = "stagingId", value = "暂存点id", dataType = "Long", required = true)
    @RequestMapping(method = RequestMethod.POST, value = "/getAlreadyInStoreYFByDeptId")
    public List<YfUserBag> getAlreadyInStoreYFByDeptId(Long stagingId) {
        List<YfUserBag> list = yfTagService.getAlreadyInStoreYFByDeptId(stagingId);
        return list;
    }


    // 查询已入库的医废列表
    @ApiOperation(value = "接口-查询已入库的医废列表")
    @RequestMapping(method = RequestMethod.POST, value = "/getInStoreYFlistByStagingId")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "hospitalId", value = "医院id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "ksId", value = "科室id", dataType = "Long", required = false),
    })
    @JSON(type = YfTag.class, include = "codeid,bag,trashType,sourceDeptId,sourceDeptName,trashTypeName,weight,intoUserId,intoUserName,intoDate")
    public TableDataInfo<List<YfTag>> getInStoreYFlistByStagingId(Long userId,Long hospitalId,Long stagingId,Long ksId) {
        if( userId == null || hospitalId == null || stagingId == null ){
            return getDataTable(new ArrayList<>());
        }
        List<YfTag> list = yfTagService.getInStoreYFlistByStagingId(stagingId,ksId);       // 按暂存点查医废状态是20的
        return getDataTable(list);
    }


    // 接口 - 选择出库 - 医废出库
    @ApiOperation(value = "接口-选择出库-医废出库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true),
            @ApiImplicitParam(name = "hospitalId", value = "医院id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", dataType = "Long", required = true),
            @ApiImplicitParam(name = "yfjson", value = "出库医废json", dataType = "String", required = true),
    })
    @RequestMapping(method = RequestMethod.POST, value = "/saveOutStoreYFlist")
    @Log(title = "接口-选择出库-医废出库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public AjaxResult saveOutStoreYFlist(Long userId, Long hospitalId, Long stagingId,String yfjson) {
        if( userId == null || hospitalId == null || stagingId == null || StringUtils.isEmpty(yfjson) ){
            return AjaxResult.error("出库失败！");
        }
        // json转list
        List<YfTag> yfList = JSONObject.parseArray(yfjson, YfTag.class);

        int i = 0;
        if( yfList != null && !yfList.isEmpty() ){
            i = yfTagService.saveOutStoreYFlist(yfList, userId);
        }

        if (i < 0) {
            return AjaxResult.error("出库失败！");
        }
        return AjaxResult.success("出库成功！");
    }



    // 根据暂存点、收集人、医废状态查询医废 -- 扫科室码页面按钮
    //a87f8d3e-c63b-4f0e-b22a-141bf31d54c3
    @ApiOperation(value = "接口-根据暂存点、收集人、医废状态查询医废")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true),
            @ApiImplicitParam(name = "yfStatus", value = "医废状态", type = "String", required = true),
            @ApiImplicitParam(name = "workId", value = "医废状态对应的工作员id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/getStatusYF")
    public List<YfTag> getStatusYF(Long stagingId, Long userId, String yfStatus, Long workId) {
        List<YfTag> list = yfTagService.getStatusYF(stagingId, userId, yfStatus, workId);
        return list;
    }


    // 根据采集人查询当日各科室各类型医废重量及袋数
    //87eabe6a-b52a-47da-9d13-f12c4bad30c5
    @ApiOperation(value = "接口-根据采集人查询当日各科室各类型医废重量及袋数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true),
            @ApiImplicitParam(name = "date", value = "查询日期yyyy-MM-dd", dataType = "Date")
    })
    @RequestMapping(method = RequestMethod.POST, value = "/getTodayEachKSYFWeightAndNumByUserId")
    @JSON(type = YfTag.class, include = "sourceDeptId,sourceDeptName,trashType,trashTypeName,weight,bag")
    public List<YfTag> getTodayEachKSYFWeightAndNumByUserId(Long stagingId, Long userId, String date) {
        if (stagingId == null || userId == null) {
            return new ArrayList<>();
        }
        // 如果没有date 或者date 格式异常，取今天
        Date selectDate;
        if (StringUtils.isEmpty(date)) {
            selectDate = new Date();
        } else {
            try {
                selectDate = new SimpleDateFormat("yyyy-MM-dd").parse(date);
            } catch (Exception e) {
                selectDate = new Date();
            }
        }
        List<YfTag> list = yfTagService.getTodayEachKSYFWeightAndNumByUserId(stagingId, userId, selectDate);
        return list;
    }


    // 查询扫码科室今天的医废列表
    @ApiOperation(value = "接口-查询扫码科室今天的医废列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "ksId", value = "科室id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/getDepartmentTodayYFTypeList")
    @JSON(type = YfTag.class, include = "trashType,trashTypeName,weight,bag")
    public List<YfTag> getDepartmentTodayYFTypeList(Long stagingId, Long ksId, Long userId) {
        if (stagingId == null || ksId == null || userId == null) {
            return new ArrayList<>();
        }
        List<YfTag> list = yfTagService.getDepartmentTodayYFTypeList(stagingId, ksId, userId);
        return list;
    }


    // 查询当前用户今天收集科室的医废列表（只显示有医废的科室）
    @ApiOperation(value = "接口-查询当前用户今天收集科室的医废列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/getAllKSTodagYFListByUserId")
    @JSON(type = YfTag.class, include = "sourceDeptId,sourceDeptName,weight,bag")
    public List<YfTag> getAllKSTodagYFListByUserId(Long stagingId, Long userId) {
        if (stagingId == null || userId == null) {
            return new ArrayList<>();
        }
        List<YfTag> list = yfTagService.getAllKSTodagYFListByUserId(stagingId, userId);
        return list;
    }


    // 根据采集人查询指定月份各科室医废重量及袋数
    //0274cb5c-92b6-4b82-a89b-52400385a084
    @ApiOperation(value = "接口-根据采集人查询指定月份各科室医废重量及袋数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true),
            @ApiImplicitParam(name = "date", value = "查询日期yyyy-MM", dataType = "Date", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/getMonthKSYFWeightAndNumByUserId")
    public List<YfHospitalMonth> getMonthKSYFWeightAndNumByUserId(Long stagingId, Long userId, String date) {
        if (stagingId == null || userId == null || StringUtils.isEmpty(date)) {
            return new ArrayList<>();
        }
        // date 格式异常
        try {
            new SimpleDateFormat("yyyy-MM").parse(date);
        } catch (Exception e) {
            return new ArrayList<>();
        }

        List<YfHospitalMonth> list = yfTagService.getMonthKSYFWeightAndNumByUserId(stagingId, userId, date);
        return list;
    }



    // 根据暂存点id或医院id查询已收集医废(按科室分组)
    //2796a7b9-9677-4786-a7ad-9d00e6236fce
    @ApiOperation(value = "接口-根据暂存点获取医废，按科室分组")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "hospitalId", value = "医院id", type = "Long", required = true),
            @ApiImplicitParam(name = "yfStatus", value = "医废状态", type = "String", required = true)

    })
    @RequestMapping(method = RequestMethod.POST, value = "/getStatusYFByDeptIdGroupByKS")
    public List<YfVehicle> getStatusYFByDeptIdGroupByKS(Long stagingId,Long hospitalId, String yfStatus) {
        if( stagingId == null || hospitalId == null || StringUtils.isEmpty(yfStatus) ){
            return new ArrayList<>();
        }
        List<YfVehicle> list = yfTagService.getStatusYFByDeptIdGroupByKS(stagingId,hospitalId,yfStatus);
        return list;
    }

    // 根据科室id及医废状态查询未入库医废总袋数及重量
    //bd9dcd66-d7b6-4037-8667-9c1e97bc53db
    @ApiOperation(value = "接口-根据科室id及医废状态查询未入库医废总袋数及重量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "ksId", value = "科室id", type = "Long", required = true),
            @ApiImplicitParam(name = "yfStatus", value = "医废状态", type = "String", required = true)

    })
    @RequestMapping(method = RequestMethod.POST, value = "/getYFTotalByKsIdAndYFStatus")
    public YfVehicle getYFTotalByKsIdAndYFStatus(Long stagingId,Long ksId, String yfStatus) {
        if( stagingId == null || ksId == null || StringUtils.isEmpty(yfStatus) ){
            return new YfVehicle();
        }
        YfVehicle yfVehicle = yfTagService.getYFTotalByKsIdAndYFStatus(stagingId,ksId,yfStatus);
        return yfVehicle;
    }


    // 根据科室id及医废状态查询医废
    //ffa1f2a6-6418-4986-83ee-714549e62eff
    @ApiOperation(value = "接口-根据科室id及医废状态查询医废")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "ksId", value = "科室id", type = "Long", required = true),
            @ApiImplicitParam(name = "yfStatus", value = "医废状态", type = "String", required = true)

    })
    @RequestMapping(method = RequestMethod.POST, value = "/getStatusYFByKSId")
    public List<YfVehicle> getStatusYFByKSId(Long stagingId,Long ksId, String yfStatus) {
        if( stagingId == null || ksId == null || StringUtils.isEmpty(yfStatus) ){
            return new ArrayList<>();
        }
        List<YfVehicle> list = yfTagService.getStatusYFByKSId(stagingId,ksId,yfStatus);
        return list;
    }


    // 整车入库 收集 -> 入库
    //93598927-93b3-463c-86df-5ddb8015da47
    @ApiOperation(value = "接口-整车入库：收集 -> 入库")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "ksId", value = "科室id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/oneKSCollToInStoreYF")
    @Log(title = "接口-整车入库：收集 -> 入库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> oneKSCollToInStoreYF(Long stagingId, Long ksId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (stagingId == null || ksId == null || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有指定科室已收集状态才能修改成已入库状态
        yfTagService.oneKSCollToInStoreYF(stagingId, ksId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "入库成功");
        return hashMap;
    }


    // 整车出库 入库 -> 出库
    //9e284bfb-948b-4d7f-8019-05c9aefa6625
    @ApiOperation(value = "接口-整车出库：入库->出库 ")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "stagingId", value = "暂存点id", type = "Long", required = true),
            @ApiImplicitParam(name = "ksId", value = "科室id", type = "Long", required = true),
            @ApiImplicitParam(name = "userId", value = "用户id", type = "Long", required = true)
    })
    @RequestMapping(method = RequestMethod.POST, value = "/oneKSInToOutStoreYF")
    @Log(title = "接口-整车出库：入库->出库", businessType = BusinessType.UPDATE, operatorType = OperatorType.MOBILE,
            requestType = RequestType.KEYVALUE, getUserId = GetUserId.USERID)
    public HashMap<String, Object> oneKSInToOutStoreYF(Long stagingId, Long ksId, Long userId) {
        HashMap<String, Object> hashMap = new HashMap<>();
        if (stagingId == null || userId == null) {
            hashMap.put("resultCode", "N");
            hashMap.put("msg", "参数不能为空");
            return hashMap;
        }
        // 只有指定科室已入库状态才能修改成已出库状态
        yfTagService.oneKSInToOutStoreYF(stagingId, ksId, userId);
        hashMap.put("resultCode", "Y");
        hashMap.put("msg", "出库成功");
        return hashMap;
    }


}
